using System.Collections;
using System.Collections.Generic;
using I2.Loc;
using UnityEngine;

public class CopsAndRobbersInterface : MonoBehaviour
{
	public static CopsAndRobbersInterface instance;

	[SerializeField]
	private List<GateHealthUI> gatesUI;

	[SerializeField]
	private UILabel notification;

	[SerializeField]
	private FloatingGatePointer floatingGatePointer;

	private void Awake()
	{
		if (GameController.instance.curTypeGame != TypeGame.CopsAndRobbers)
		{
			base.gameObject.SetActive(false);
		}
		else
		{
			instance = this;
		}
	}

	private void OnDestroy()
	{
		instance = null;
	}

	public void ResetUI(List<CRGate> gates)
	{
		if (notification != null)
		{
			notification.gameObject.SetActive(false);
		}
		if (gatesUI.Count != gates.Count)
		{
			BCWDebug.LogError("Не подкинуты ссылки на ворота в интерфейс.");
			return;
		}
		for (int i = 0; i < gates.Count; i++)
		{
			gatesUI[i].Init(gates[i].maxHealth);
			gatesUI[i].SetHealthUI(gates[i].maxHealth);
			gatesUI[i].SetGateSemitransparent(true);
			gatesUI[i].textOverImage.text = ((!LocalizationManager.IsRussian()) ? string.Format("G{0}", i + 1) : string.Format("В{0}", i + 1));
		}
		gatesUI[0].SetGateSemitransparent(false);
		ChangeFloatingPoint();
		UpdateAllInterface(gates);
	}

	public void GateDamaged(int currentHealth)
	{
		int num = CopsAndRobbersController.instance.GetCurrentStage();
		if (num < 0)
		{
			num = gatesUI.Count - 1;
		}
		gatesUI[num].SetHealthUI(currentHealth);
		UpdateFloatingGate();
	}

	public void ShowGateDamaged()
	{
		int num = CopsAndRobbersController.instance.GetCurrentStage();
		if (num < 0)
		{
			num = gatesUI.Count - 1;
		}
		gatesUI[num].ShowHit();
		floatingGatePointer.ShowHit();
	}

	public void GateDestroyed(int gateIndex)
	{
		if (gateIndex < 0)
		{
			gateIndex = gatesUI.Count - 1;
		}
		gatesUI[gateIndex].SetGateDestroyedUI();
		UpdateFloatingGate();
		if (gateIndex < gatesUI.Count - 1 && gateIndex >= 0)
		{
			gatesUI[gateIndex + 1].SetGateSemitransparent(false);
		}
	}

	public void UpdateAllInterface(List<CRGate> gates)
	{
		for (int num = gates.Count - 1; num >= 0; num--)
		{
			if (gates[num].health > 0)
			{
				GateDamaged(gates[num].health);
			}
			else
			{
				GateDestroyed(num);
			}
		}
		UpdateFloatingGate();
	}

	public void EnableGatePointer()
	{
		floatingGatePointer.gameObject.SetActive(true);
	}

	public void ChangeFloatingPoint()
	{
		EnableGatePointer();
		List<CRGate> gates = CopsAndRobbersController.instance.gates;
		int currentStage = CopsAndRobbersController.instance.GetCurrentStage();
		if (currentStage < 0)
		{
			PointerController.instance.RemoveAllPointers();
			floatingGatePointer.gameObject.SetActive(false);
		}
		else
		{
			floatingGatePointer.Init((!LocalizationManager.IsRussian()) ? string.Format("G{0}", currentStage + 1) : string.Format("В{0}", currentStage + 1));
			PointerController.instance.RemoveAllPointers();
			PointerController.instance.AddPointer(gates[currentStage].transform.position + Vector3.up * 1.5f, floatingGatePointer.gameObject, null);
		}
		UpdateFloatingGate();
	}

	public void UpdateFloatingGate()
	{
		int num = CopsAndRobbersController.instance.GetCurrentStage();
		if (num < 0)
		{
			num = gatesUI.Count - 1;
		}
		CRGate cRGate = CopsAndRobbersController.instance.gates[num];
		floatingGatePointer.SetHealth(cRGate.health, cRGate.maxHealth);
	}

	public IEnumerator ShowNotification(PunTeams.Team myTeam, int currentGateIndex, float duration)
	{
		if (notification == null)
		{
			yield break;
		}
		notification.gameObject.SetActive(true);
		switch (myTeam)
		{
		case PunTeams.Team.blue:
			switch (currentGateIndex)
			{
			case 0:
				notification.text = ScriptLocalization.Get("Game_Common/sm_CopsAndRobbers_robbJoinMatch");
				break;
			case 1:
				notification.text = ScriptLocalization.Get("Game_Common/sm_CopsAndRobbers_gate1robb");
				break;
			case 2:
				notification.text = ScriptLocalization.Get("Game_Common/sm_CopsAndRobbers_gate2robb");
				break;
			case -1:
				notification.text = string.Empty;
				break;
			default:
				notification.text = "Wrong stage index";
				break;
			}
			break;
		case PunTeams.Team.red:
			switch (currentGateIndex)
			{
			case 0:
				notification.text = ScriptLocalization.Get("Game_Common/sm_CopsAndRobbers_copsJoinMatch");
				break;
			case 1:
				notification.text = ScriptLocalization.Get("Game_Common/sm_CopsAndRobbers_gate1cops");
				break;
			case 2:
				notification.text = ScriptLocalization.Get("Game_Common/sm_CopsAndRobbers_gate2cops");
				break;
			case -1:
				notification.text = string.Empty;
				break;
			default:
				notification.text = "Wrong stage index";
				break;
			}
			break;
		}
		yield return new WaitForSeconds(duration);
		notification.gameObject.SetActive(false);
	}
}
